import 'package:Flutter_Demo/core/model/meal_model.dart';
import 'package:Flutter_Demo/core/viewmodel/favor_view_model.dart';
import 'package:Flutter_Demo/ui/pages/detail/detail.dart';
import 'package:Flutter_Demo/ui/widgets/operation_item.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';


final double cardRadus = 12;

class MYMealItem extends StatelessWidget {
  final MYMealModel _mealModel;

  MYMealItem(this._mealModel);
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: Card(
        margin: EdgeInsets.all(10),
        color: Colors.white,
        // 阴影
        elevation: 4,
        // 圆角
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(cardRadus)),
        child: Column(
          children: <Widget>[
            buildBasicInfo(),
            buildOprationInfo()
          ],
        ),
      ),
      onTap: () {
        Navigator.of(context).pushNamed(MYDetailScreen.routeName, arguments: _mealModel);
      },
    );
  }

  Widget buildBasicInfo() {
    return Stack(
      children: <Widget>[
        ClipRRect(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(cardRadus),
            topRight: Radius.circular(cardRadus),
          ),
          child: Image.network(_mealModel.imageUrl, width: double.infinity, height: 250, fit: BoxFit.cover,),
        ),
        Positioned(
          right: 10,
          bottom: 10,
          child: Container(
//            width: 300,
            padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            decoration: BoxDecoration(
              color: Colors.black54,
              borderRadius: BorderRadius.circular(6),

            ),
            child: Text(_mealModel.title, style: TextStyle(color: Colors.white, fontSize: 24)),
          ),
        )
      ],
    );
  }

  Widget buildOprationInfo() {
    return Padding(
      padding: EdgeInsets.all(4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          MYOperationItem(Icon(Icons.schedule), "${_mealModel.duration}分钟"),
          MYOperationItem(Icon(Icons.restaurant), "${_mealModel.complexityString}"),
          buildFavorItem(),
        ],
      ),
    );
  }



  Widget buildFavorItem() {
    return Consumer<MYFavorViewModel>(
      builder: (ctx, favorViewModel, child) {
        final iconData = favorViewModel.isFavor(_mealModel) ? Icon(Icons.favorite, color: Colors.red,) : Icon(Icons.favorite_border, color: Colors.black,);
        final title = favorViewModel.isFavor(_mealModel) ? "已收藏" : "未收藏";
        return GestureDetector(
          child: MYOperationItem(iconData, title),
          onTap: () {
             favorViewModel.handleMeal(_mealModel);
          },
        );
      }
    );
  }



}




